package com.xinghou.platform.dao.main.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.main.SharedCardBrowseDAO;
import com.xinghou.platform.model.main.SharedCardBrowse;

@Service
public class SharedCardBrowseDAOImpl  {/*
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	public SharedCardBrowse getSharedCardBrowseByid(Integer Id)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT 	Id,User_id,Card_id,Browse_type,Create_time,Update_time FROM shared_card_browse where Id = ?";
		SharedCardBrowse sharedCardBrowse = jdbcTemplate.queryForObject(sql, new Object[] {Id}, new SharedCardBrowseMapper());
		CustomerContextHolder.clearCustomerType();
		return sharedCardBrowse;
	}

	public int updateSharedCardBrowse(SharedCardBrowse sharedCardBrowse)
			throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"UPDATE shared_card_browse SET User_id =? ,Card_id =?,Browse_type =?, Create_time =?, Update_time = ? Where Id =?"	
				);
		Object[] params = {sharedCardBrowse.getUserId(), sharedCardBrowse.getCardId(), sharedCardBrowse.getBrowseType(), 
				sharedCardBrowse.getCreateTime(), sharedCardBrowse.getUpdateTime(), sharedCardBrowse.getId()
		};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret = jdbcTemplate.update(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}

	public int saveSharedCardBrowse(SharedCardBrowse sharedCardBrowse)
			throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"INSERT INTO shared_card_browse (User_id,Card_id,Browse_type,Create_time, Update_time)VALUES(?,?,?,?,?)"
				);
		Object[] params = {sharedCardBrowse.getUserId(), sharedCardBrowse.getCardId(), sharedCardBrowse.getBrowseType(), 
				sharedCardBrowse.getCreateTime(),sharedCardBrowse.getUpdateTime()
		};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret =jdbcTemplate.save(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}
	
	public SharedCardBrowse getSharedCardBrowseBytwo(Integer userId,
			     Integer cardId) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT 	Id,User_id,Card_id,Browse_type,Create_time,Update_time FROM shared_card_browse where User_id = ? and Card_id=?";
		SharedCardBrowse sharedCardBrowse = jdbcTemplate.queryForObject(sql, new Object[] {userId,cardId}, new SharedCardBrowseMapper());
		CustomerContextHolder.clearCustomerType();
		return sharedCardBrowse;
	}
	
	public int getSharedCardBrowseByCardIdBrowseType(Integer browseType,
			Integer cardId) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT 	Id,User_id,Card_id,Browse_type,Create_time,Update_time FROM shared_card_browse where Card_id = ? and Browse_type=?";
        int num=jdbcTemplate.queryForInt(sql, new Object[] {cardId,browseType});
		CustomerContextHolder.clearCustomerType();
		return num;
	}
	private static final class SharedCardBrowseMapper implements RowMapper<SharedCardBrowse> {
		public SharedCardBrowse mapRow(ResultSet rs, int rowNum) throws SQLException {
			SharedCardBrowse sharedCardBrowse = new SharedCardBrowse();
			sharedCardBrowse.setBrowseType(rs.getShort("Browse_type"));
			sharedCardBrowse.setCardId(rs.getInt("Card_id"));
			sharedCardBrowse.setCreateTime(rs.getTimestamp("Create_time"));
			sharedCardBrowse.setUpdateTime(rs.getTimestamp("Update_time"));
			sharedCardBrowse.setId(rs.getInt("Id"));
			sharedCardBrowse.setUserId(rs.getInt("User_id"));
			return sharedCardBrowse;
		}
	}
	
	public List<SharedCardBrowse> getSharedCardBrowse(int num, int cardid) {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		 String sql = "SELECT Id,User_id,Card_id,Browse_type,Create_time,Update_time FROM shared_card_browse where Card_id=? and Id>? ORDER BY Id LIMIT 10;";
		  List<SharedCardBrowse> SharedCardBrowselist=(List<SharedCardBrowse>)jdbcTemplate.query(sql,new Object[] { cardid, num },new SharedCardBrowseMapper());
		return SharedCardBrowselist;
	}

	public List<SharedCardBrowse> getListByCardidTime(int cardid,
			Timestamp time, int rowNum) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		 String sql = "SELECT Id,User_id,Card_id,Browse_type,Create_time,Update_time FROM shared_card_browse where Card_id=? and Update_time<? ORDER BY Update_time DESC LIMIT ?;";
		  List<SharedCardBrowse> SharedCardBrowselist=(List<SharedCardBrowse>)jdbcTemplate.query(sql,new Object[] { cardid, time,rowNum },new SharedCardBrowseMapper());
		return SharedCardBrowselist;
	}*/
	
}
